home *** CD-ROM | disk | FTP | other *** search
/ Aminet 12 / Aminet 12 (1996)(GTI - Schatztruhe)[!][Jun 1996].iso / Aminet / gfx / show / gs_src_amiga.lha / geninit.c < prev    next >
C/C++ Source or Header  |  1996-01-18  |  6KB  |  226 lines

  1. /* Copyright (C) 1995 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* geninit.c */
  20. /* Utility for merging all the Ghostscript initialization files */
  21. /* (gs_*.ps) into a single file, optionally converting them to C data. */
  22. #include "stdio_.h"
  23. #include "string_.h"
  24.  
  25. /* Usage:
  26.  *    geninit <init-file.ps> <gconfig.h> <merged-init-file.ps>
  27.  *    geninit <init-file.ps> <gconfig.h> -c <merged-init-file.c>
  28.  */
  29.  
  30. /* Forward references */
  31. private void merge_to_c();
  32. private void merge_to_ps();
  33.  
  34. #define line_size 128
  35.  
  36. int
  37. main(int argc, char *argv[])
  38. {    const char *fin;
  39.     FILE *in;
  40.     const char *fconfig;
  41.     FILE *config;
  42.     const char *fout;
  43.     FILE *out;
  44.     bool to_c = false;
  45.  
  46.     if ( argc == 4 )
  47.       fin = argv[1], fconfig = argv[2], fout = argv[3];
  48.     else if ( argc == 5 && !strcmp(argv[3], "-c") )
  49.       fin = argv[1], fconfig = argv[2], fout = argv[4], to_c = true;
  50.     else
  51.       { fprintf(stderr, "\
  52. Usage: geninit gs_init.ps gconfig.h gs_xinit.ps\n\
  53.  or    geninit gs_init.ps gconfig.h -c gs_init.c\n");
  54.         exit(1);
  55.       }
  56.     in = fopen(fin, "r");
  57.     if ( in == 0 )
  58.       { fprintf(stderr, "Cannot open %s for reading.\n", fin);
  59.         exit(1);
  60.       }
  61.     config = fopen(fconfig, "r");
  62.     if ( config == 0 )
  63.       { fprintf(stderr, "Cannot open %s for reading.\n", fconfig);
  64.         fclose(in);
  65.         exit(1);
  66.       }
  67.     out = fopen(fout, "w");
  68.     if ( out == 0 )
  69.       { fprintf(stderr, "Cannot open %s for writing.\n", fout);
  70.         fclose(config);
  71.         fclose(in);
  72.         exit(1);
  73.       }
  74.     if ( to_c )
  75.       merge_to_c(in, config, out);
  76.     else
  77.       merge_to_ps(in, config, out);
  78.     fclose(out);
  79.     return 0;
  80. }
  81.  
  82. /* Read a line from the input. */
  83. private bool
  84. rl(FILE *in, char *str, int len)
  85. {    if ( fgets(str, len, in) == NULL )
  86.       return false;
  87.     str[strlen(str) - 1] = 0;    /* remove newline */
  88.     return true;
  89. }
  90.  
  91. /* Write a line on the output. */
  92. private void
  93. wl(FILE *out, const char *str, bool to_c)
  94. {    if ( to_c )
  95.       { int n = 0;
  96.         const char *p = str;
  97.         for ( ; *p; ++p )
  98.           { char c = *p;
  99.         const char *format = "%d,";
  100.         if ( c >= 32 && c < 127 )
  101.           format = (c == '\'' || c == '\\' ? "'\\%c'," : "'%c',");
  102.         fprintf(out, format, c);
  103.         if ( ++n == 15 )
  104.           { fputs("\n", out);
  105.             n = 0;
  106.           }
  107.           }
  108.         fputs("10,\n", out);
  109.       }
  110.     else
  111.       { fprintf(out, "%s\n", str);
  112.       }
  113. }
  114.  
  115. /* Strip whitespace and comments from a string if possible. */
  116. /* Return a pointer to any string that remains, or NULL if none. */
  117. /* Note that this may store into the string. */
  118. private char *
  119. doit(char *line)
  120. {    char *str = line;
  121.     char *p1;
  122.  
  123.     while ( *str == ' ' || *str == '\t' )    /* strip leading whitespace */
  124.       ++str;
  125.     if ( *str == 0 )        /* all whitespace */
  126.       return NULL;
  127.     if ( !strncmp(str, "%END", 4) )    /* keep these for .skipeof */
  128.       return str;
  129.     if ( str[0] == '%' )        /* comment line */
  130.       return NULL;
  131.     if ( (p1 = strchr(str, '%')) == NULL )    /* no internal comment */
  132.       return str;
  133.     if ( strchr(p1, ')') != NULL )    /* might be a % inside a string */
  134.       return str;
  135.     while ( p1[-1] == ' ' || p1[-1] == '\t' )
  136.       --p1;
  137.     *p1 = 0;            /* remove comment */
  138.     return str;
  139. }
  140.  
  141. /* Merge a file from input to output. */
  142. private void
  143. mergefile(FILE *in, FILE *config, FILE *out, bool to_c)
  144. {    char line[line_size + 1];
  145.  
  146.     while ( rl(in, line, line_size) )
  147.       { char psname[line_size + 1];
  148.         int nlines;
  149.  
  150.         if ( !strncmp(line, "%% Replace ", 11) &&
  151.          sscanf(line + 11, "%d %s", &nlines, psname) == 2
  152.            )
  153.           { while ( nlines-- > 0 )
  154.           rl(in, line, line_size);
  155.         if ( psname[0] == '(' )
  156.           { FILE *ps;
  157.             psname[strlen(psname) - 1] = 0;
  158.             ps = fopen(psname + 1, "r");
  159.             if ( ps == 0 )
  160.               { fprintf(stderr, "Cannot open %s for reading.\n", psname + 1);
  161.             exit(1);
  162.               }
  163.             mergefile(ps, config, out, to_c);
  164.           }
  165.         else if ( !strcmp(psname, "INITFILES") )
  166.           { /*
  167.              * We don't want to bind config.h into geninit, so
  168.              * we parse it ourselves at execution time instead.
  169.              */
  170.             rewind(config);
  171.             while ( rl(config, psname, line_size) )
  172.               if ( !strncmp(psname, "psfile_(\"", 9) )
  173.             { FILE *ps;
  174.               psname[strlen(psname) - 2] = 0;
  175.               ps = fopen(psname + 9, "r");
  176.               if ( ps == 0 )
  177.                 { fprintf(stderr, "Cannot open %s for reading.\n", psname + 9);
  178.                   exit(1);
  179.                 }
  180.               mergefile(ps, config, out, to_c);
  181.             }
  182.           }
  183.         else
  184.           { fprintf(stderr, "Unknown %%%% Replace %d %s\n",
  185.                 nlines, psname);
  186.             exit(1);
  187.           }
  188.           }
  189.         else
  190.           { char *str = doit(line);
  191.         if ( str != 0 )
  192.           wl(out, str, to_c);
  193.           }
  194.       }
  195.     fclose(in);
  196. }
  197.  
  198. /* Merge and produce a C file. */
  199. private void
  200. merge_to_c(FILE *in, FILE *config, FILE *out)
  201. {    char line[line_size + 1];
  202.  
  203.     fputs("/*\n", out);
  204.     while ( (rl(in, line, line_size), line[0]) )
  205.       fprintf(out, "%s\n", line);
  206.     fputs("*/\n", out);
  207.     fputs("\n", out);
  208.     fputs("/* Pre-compiled interpreter initialization string. */\n", out);
  209.     fputs("#include \"stdpre.h\"\n", out);
  210.     fputs("\n", out);
  211.     fputs("const byte gs_init_string[] = {\n", out);
  212.     mergefile(in, config, out, true);
  213.     fputs("10};\n", out);
  214.     fputs("const uint gs_init_string_sizeof = sizeof(gs_init_string);\n", out);
  215. }
  216.  
  217. /* Merge and produce a PostScript file. */
  218. private void
  219. merge_to_ps(FILE *in, FILE *config, FILE *out)
  220. {    char line[line_size + 1];
  221.  
  222.     while ( (rl(in, line, line_size), line[0]) )
  223.       fprintf(out, "%s\n", line);
  224.     mergefile(in, config, out, false);
  225. }
  226.